'use strict';

const Service = require('egg').Service;

class CartService extends Service {
    async findAll(uid) {
        const sql = 'select * from cart where uid = ?';
        const params = [uid];
        const res = await this.app.mysql.query(sql, params);
        return res;
    }

    async create(gid, num, uid) {
        const sql = 'insert into cart (gid, num, uid) values (?, ?, ?)';
        const parmas = [gid, num, uid];
        const res = await this.app.mysql.query(sql, parmas);
        return res;
    }

    async find(gid, uid) {
        const sql = 'select * from cart where gid = ? and uid = ? limit 0, 1';
        const params = [gid, uid];
        const res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    async update(gid, num, uid) {
        const sql = 'update cart set num = ? where gid = ? and uid = ?';
        const params = [num, gid, uid];
        const res = await this.app.mysql.query(sql, params);
        return res;
    }

    async remove(gid, uid) {
        const sql = 'delete from cart where gid = ? and uid = ?';
        const params = [gid, uid];
        const res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = CartService;
